import {
    AfterViewInit,
    ChangeDetectionStrategy,
    ChangeDetectorRef,
    Component,
    ElementRef,
    OnDestroy,
}                                  from '@angular/core';
import { ActivationEnd, Router }   from '@angular/router';
import { fromEvent, Subscription } from 'rxjs';
import { debounceTime, filter }    from 'rxjs/operators';

@Component({
    selector       : 'app-account-settings',
    templateUrl    : './settings.component.html',
    styleUrls      : [ './settings.component.less' ],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProAccountSettingsComponent implements AfterViewInit, OnDestroy
{
    private resize$: Subscription;
    private router$: Subscription;
    mode = 'inline';
    title: string;
    user: any;
    menus: any[] = [
        {
            key  : 'base',
            title: '基本设置',
        },
        {
            key  : 'security',
            title: '安全设置',
        },
        {
            key  : 'binding',
            title: '账号绑定',
        },
        {
            key  : 'notification',
            title: '新消息通知',
        },
    ];

    constructor( private router: Router, private cdr: ChangeDetectorRef, private el: ElementRef )
    {
        this.router$ = this.router.events.pipe(filter(e => e instanceof ActivationEnd)).subscribe(() => this.setActive());
    }

    private setActive()
    {
        const key = this.router.url.substr(this.router.url.lastIndexOf('/') + 1);
        this.menus.forEach(i =>
        {
            i.selected = i.key === key;
        });
        this.title = this.menus.find(w => w.selected).title;
    }

    to( item: any )
    {
        this.router.navigateByUrl(`/pro/settings/${ item.key }`);
    }

    private resize()
    {
        const el = this.el.nativeElement as HTMLElement;
        let mode = 'inline';
        const { offsetWidth } = el;
        if ( offsetWidth < 641 && offsetWidth > 400 )
        {
            mode = 'horizontal';
        }
        if ( window.innerWidth < 768 && offsetWidth > 400 )
        {
            mode = 'horizontal';
        }
        this.mode = mode;
        this.cdr.detectChanges();
    }

    ngAfterViewInit(): void
    {
        this.resize$ = fromEvent(window, 'resize')
            .pipe(debounceTime(200))
            .subscribe(() => this.resize());
    }

    ngOnDestroy(): void
    {
        this.resize$.unsubscribe();
        this.router$.unsubscribe();
    }
}
